package com.wizinno.android.urineflow.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import cn.hutool.core.util.ArrayUtil;
import com.aill.androidserialport.ByteUtil;
import com.wizinno.android.urineflow.R;
import com.wizinno.android.urineflow.application.ExitApplication;
import com.wizinno.android.urineflow.constant.ActionConstant;
import com.wizinno.android.urineflow.constant.CommonControl;
import com.wizinno.android.urineflow.serialport.SerialPortUtil;
import com.wizinno.android.urineflow.service.MQTTService;
import com.wizinno.android.urineflow.util.APKVersionUtils;

public class SystemSettingActivity extends BaseActivity {

    private TextView tv_device_sn;
    private TextView tv_android_version;

    private static TextView tv_mqtt_state;
    private TextView tv_device_state;

    private TextView tv_firmware_version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ExitApplication.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_setting);

        tv_device_sn = findViewById(R.id.tv_device_sn);
        tv_android_version = findViewById(R.id.tv_android_version);
        tv_mqtt_state = findViewById(R.id.tv_mqtt_state);
        tv_device_state = findViewById(R.id.tv_device_state);
        tv_firmware_version = findViewById(R.id.tv_firmware_version);

        setDeviceVersion();
        initView();
    }

    private void initView() {
        if (CommonControl.INSTANCE != null) {
            tv_device_sn.setText(CommonControl.INSTANCE.DEVICE_SN);
            tv_android_version.setText(APKVersionUtils.getVersionName(this));
            setMqttState();
        }
    }

    private void setDeviceState() {
        if (tv_device_state != null) {
            tv_device_state.setText(CommonControl.INSTANCE.isUsbNormal ? "已连接" : "未连接");
        }
    }

    private void setDeviceVersion() {
        String version = "";
        if (ArrayUtil.isNotEmpty(CommonControl.INSTANCE.a1Version)) {
            String a1 = "A1版本：" + ByteUtil.hexBytesToString(CommonControl.INSTANCE.a1Version);
            version += a1 + "\n";
        }
        if (ArrayUtil.isNotEmpty(CommonControl.INSTANCE.a2Version)) {
            String a2 = "A2版本：" + ByteUtil.hexBytesToString(CommonControl.INSTANCE.a2Version);
            version += a2 + "\n";
        }
        //todo A3
        tv_firmware_version.setText(version);
        setDeviceState();
    }

    public static void setMqttState() {
        if (tv_mqtt_state != null) {
            tv_mqtt_state.setText(CommonControl.INSTANCE.isMqttNormal ? "已联网" : "未联网");
        }
    }

    public void toHome(View view) {
        Intent intent = new Intent(SystemSettingActivity.this, MainActivity.class);
        startActivity(intent);
    }

    public void toSystem(View view) {
        Intent intent = new Intent(SystemSettingActivity.this, SettingBaseActivity.class);
        intent.setAction(ActionConstant.SYSTEM);
        startActivity(intent);
    }

    public void toSteering(View view) {
        Intent intent = new Intent(SystemSettingActivity.this, SettingBaseActivity.class);
        intent.setAction(ActionConstant.STEERING);
        startActivity(intent);
    }

    public void toDistance(View view) {
        Intent intent = new Intent(SystemSettingActivity.this, SettingBaseActivity.class);
        intent.setAction(ActionConstant.DISTANCE);
        startActivity(intent);
    }

    public void toWeight(View view) {
        Intent intent = new Intent(SystemSettingActivity.this, SettingBaseActivity.class);
        intent.setAction(ActionConstant.WEIGHT);
        startActivity(intent);
    }

    public void androidUpdate(View view) {
        //todo 检查更新
    }

    public void usbReconnect(View view) {
        SerialPortUtil.readVersion();
        setDeviceVersion();
    }

    public void mqttReconnect(View view) {
        ComponentName componentName = startService(new Intent(this, MQTTService.class));
        if (componentName != null) {
            tv_mqtt_state.setText(CommonControl.INSTANCE.isMqttNormal ? "已联网" : "未联网");
        }
    }

    public void exit(View view) {
        ExitApplication.getInstance().exit();
    }
}